const { ipcMain, BrowserWindow } = require("electron");

const getSource = (url) => {
   return new Promise((resolve,reject)=>{
    // 创建内存窗口，不显示
    const win = new BrowserWindow({
        width: 500,
        height: 500,
        fullscreen:true,
        show: false,
        webPreferences: {
          offscreen: true,
        },
      });
      win.loadURL(url);
      win.webContents.on("did-finish-load", async () => {
        const title = win.getTitle();
        try {
          // nativeImage
          const image = await win.webContents.capturePage();
          if(image.isEmpty()){
            resolve({
              msg:'没有该【'+url+'】网站'
            })
          }
          const screenshot = image.toDataURL();
          resolve({
              title,
              screenshot,
              url
          })
        } catch (e) {
          console.log(e)
          reject(e)
        }
      });
   })
};

// 异步操作
ipcMain.handle("on-url-event", async(e, url) => {
    const result = await getSource(url)
    return result
});

